package com.ddwl.user.service;

import com.ddwl.schema.bo.user.SetUserAgentRegionBo;
import org.springframework.util.CollectionUtils;
import com.ddwl.common.exception.ParamException;
import com.ddwl.schema.vo.user.UserVo;
import com.ddwl.user.dao.ProfitRegionDao;
import com.ddwl.user.dao.model.ProfitRegion;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类ProfitRegionServer.java的实现描述：区域推广服务
 *
 * @author zach
 * @Date :2020-06-24 16:39.
 */
@Service
public class ProfitRegionServer {

     @Resource
     public ProfitRegionDao profitRegionDao;
     @Resource
     public UserService userService;

    /**
     * 查询已经被代理的区域
     * @return
     */
     public List<String> getAgentRegion(){
         List<ProfitRegion> list = profitRegionDao.getProfitRegion();
         if(!CollectionUtils.isEmpty(list)){
             return list.stream().map(ProfitRegion::getRegionId).collect(Collectors.toList());
         }
         return null;
     }

    /**
     * 查询用户代理区域
     * @param userId
     * @return
     */
     public List<String> getUserRegion(String userId){
         List<ProfitRegion> list = profitRegionDao.getProfitRegion(userId);
         if(!CollectionUtils.isEmpty(list)){
             return list.stream().map(ProfitRegion::getRegionId).collect(Collectors.toList());
         }
         return null;
     }

    /**
     * 查询代理区域的用户信息
     * @param regionId
     * @return
     */
    public UserVo getRegionUser(String regionId){
        List<ProfitRegion> list = profitRegionDao.getProfitRegionByRegion(regionId);
        UserVo vo=null;
        if(!CollectionUtils.isEmpty(list)){
            vo= userService.getSimpleUserById(list.get(0).getUserId());
        }
        if(vo==null){
            vo = userService.getCompayAccount();
        }
        return vo;
    }

    /**
     * 根据区域id查询该区域的总代理
     * @param regionId
     * @return
     */
    public UserVo getTopUserByRegionId(String regionId) {
        List<ProfitRegion> list = profitRegionDao.getProfitRegionByRegion(regionId);
        if(!CollectionUtils.isEmpty(list)){
            return userService.getSimpleUserById(list.get(0).getUserId());
        }
        return null;
    }

    /**
     * 设置用户代理的区域
     */
    @Transactional(rollbackFor = Exception.class)
    public void setUserAgentRegion(SetUserAgentRegionBo agentRegion){
        ParamException.isNull(userService.getById(agentRegion.getUserId()),"请输入正确的用户Id");
        ParamException.isTrue(!CollectionUtils.isEmpty(profitRegionDao.getProfitRegionByRegionNotUserId(agentRegion.getRegionId(),agentRegion.getUserId())),"区域被绑定请输入未绑定区域");
        profitRegionDao.deleteProfitRegion(agentRegion.getUserId());
        profitRegionDao.addProfitRegion(agentRegion.getRegionId(),agentRegion.getUserId());
        userService.updateUserAgent(agentRegion.getUserId(),agentRegion.getRegionId());
     }


}
